<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../../assets/css/base.css">
</head>
<body>
    <div id="app">
        <pre v-pre class="note">
        自动化数据传递:自动事件绑定，自动事件触发
            + 保证 自动事件绑定优先于自动事件触发执行
        </pre>
        <comp-a></comp-a>
        <comp-b></comp-b>
    </div>

    <!-- 组件A-A -->
    <script type="text/x-template" id="compAa">
        <div class="cbox">
            <h5>组件A-A</h5>
            <p>msg：{{ msg }}</p>
            <input type="button" value="emitMittEvent" @click="emitMittEvent()">
        </div>
    </script>
    <!-- 组件A -->
    <script type="text/x-template" id="compA">
        <div class="cbox">
            <h5>组件A</h5>
            <comp-aa></comp-aa>
        </div>
    </script>

    <!-- 组件B-A -->
    <script type="text/x-template" id="compBa">
        <div class="cbox">
            <h5>组件B-A</h5>
            <p>tempMsg:{{ tempMsg }}</p>
            <input type="button" value="bindMittEvent" @click="bindMittEvent()">
        </div>
    </script>
    <!-- 组件B -->
    <script type="text/x-template" id="compB">
        <div class="cbox">
            <h5>组件B</h5>
            <comp-ba></comp-ba>
        </div>
    </script>

    <!-- <script src="../../assets/mitt/mitt.js"></script> -->
    <script type="module">
        import { createApp } from "../../assets/vue3/vue.esm-browser.js";
        // 加载独立事件工具函数
        import mitt from "../../assets/mitt/mitt.mjs";
        // 加载了事件构成对象
        let emitter = mitt()
        console.log(emitter);

        const CompAa = {
            template:"#compAa",
            data() {
                return {
                    msg: "组件a-a的数据变量 msg"
                }
            },
            methods: {
                emitMittEvent() {
                    console.log("CompAa-事件触发")
                    emitter.emit("update:msg",this.msg)
                }
            },
            mounted () {
                this.emitMittEvent();
            },
        }
        const CompBa = {
            template:"#compBa",
            data() {
                return {
                    tempMsg: ""
                }
            },
            methods: {
                bindMittEvent() {
                    console.log("CompBa-事件绑定")
                    emitter.on("update:msg",(e)=>{
                        console.log("组件B-a",e)
                        this.tempMsg = e;
                    })
                }
            },
            // mounted () {
            //     this.bindMittEvent();
            // },
            beforeMount () {
                this.bindMittEvent();
            },
        }
        const CompA = {
            template:"#compA",
            components: {
                CompAa,
            }
        }
        const CompB = {
            template:"#compB",
            components: {
                CompBa,
            },
        }
        createApp({
            components: {
                CompA,CompB
            },
            data() {
                return {
                    
                }
            }
        }).mount("#app")
    </script>
</body>
</html>